/**
 * @FileName: SplashScreenActivity.java
 * @Package:com.bdtop.dbl.ui.activities.main
 * @Description: TODO
 * @author: MingjunLi
 * @date:2014-9-15
 * @version V1.0
 */

package com.bdtop.dbl.ui.activities.main;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.bdtop.dbl.BaseActivity;
import com.bdtop.dbl.CrashHandler;
import com.bdtop.dbl.R;
import com.bdtop.dbl.bluetooth.BleDeviceWarningActivity;
import com.bdtop.dbl.bluetooth.BluetoothLeService;
import com.bdtop.dbl.server.ServerApiFactory;
import com.bdtop.dbl.server.response.LoginResponse;
import com.bdtop.dbl.thirdparty.asynchttp.RequestCallback;
import com.bdtop.dbl.ui.activities.acccount.LoginActivity;
import com.bdtop.dbl.ui.activities.me.MsgActivity;
import com.bdtop.dbl.utils.Constants;
import com.bdtop.dbl.utils.DBLDebug;
import com.bdtop.dbl.utils.NetworkUtils;
import com.bdtop.dbl.utils.Preferences;

/**
 * @ClassName: SplashScreenActivity
 * @Description: TODO
 * @author: MingjunLi
 * @date:2014-9-15
 */
public class SplashScreenActivity extends BaseActivity {

	public static final void launch(Context context) {
		Intent intent = new Intent(context, SplashScreenActivity.class);
		context.startActivity(intent);
	}

	public static final void launch(Context context, int what) {
		Intent intent = new Intent(context, SplashScreenActivity.class);
		intent.putExtra("what", what);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash_screen);

		
	}
	
	@Override protected void onResume() {
		super.onResume();
		final Handler myHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if (msg.what == 99999) {
					DBLDebug.logd("SplashScreenActivity launch RuntimeCheckActivity");
					RuntimeCheckActivity.launch(SplashScreenActivity.this);
				} else if (msg.what == 2) {
					// 去登录
					if (Preferences.isFromPush(SplashScreenActivity.this)) {
						// 新起一个 登录页面 FLAG_ACTIVITY_NEW_TASK
						DBLDebug.logd("SplashScreenActivity isFromPush true");
						DBLDebug.logd("SplashScreenActivity launch login");
						Intent intent = new Intent(SplashScreenActivity.this, LoginActivity.class);
						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						startActivity(intent);
					} else {
						// launch 登录页面
						DBLDebug.logd("SplashScreenActivity isFromPush false");
						DBLDebug.logd("SplashScreenActivity launch login");
						LoginActivity.launch(SplashScreenActivity.this);
					}
					Preferences.saveFromPush(SplashScreenActivity.this, false);

				}
				SplashScreenActivity.this.finish();
			}
		};
		// 来自推送
		if (Preferences.isFromPush(this)) {
			DBLDebug.logd("SplashScreenActivity isFromPush true");
			if (Preferences.isLogin(this)) {
				// 已经登录 ，去消息
				DBLDebug.logd("SplashScreenActivity isLogin true launch MsgActivity");
				Intent intent = new Intent(this, MsgActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent);
				this.finish();
				return;
			} else {
				// 尚未登录
				DBLDebug.logd("SplashScreenActivity isLogin false");
				String username = Preferences.getUserName(SplashScreenActivity.this);
				String passwoed = Preferences.getPassword(SplashScreenActivity.this);
				// 自动登录
				if (!"".equals(username) && !"".equals(passwoed)) {
					DBLDebug.logd("SplashScreenActivity to request login");
					login(username, passwoed);
				} else {
					// 没有账号密码去登录 页面
					myHandler.sendEmptyMessageDelayed(2, 1000);
				}
			}
		} else {
			DBLDebug.logd("SplashScreenActivity isFromPush false");
			// 不是推送
			checkPreferncesWorkState(); // fix
			if (Preferences.isFirstRunning(SplashScreenActivity.this)) {
				// 第一次登录 去 Check 页面
				myHandler.sendEmptyMessageDelayed(99999, 1000);
			} else if (Preferences.isAutoLogin(SplashScreenActivity.this)) {
				// 是否是自动登录
				DBLDebug.logd("SplashScreenActivity isAutoLogin true");
				String username = Preferences.getUserName(SplashScreenActivity.this);
				String passwoed = Preferences.getPassword(SplashScreenActivity.this);
				if (!"".equals(username) && !"".equals(passwoed)) {
					// 自动登录
					DBLDebug.logd("SplashScreenActivity to request login");
					login(username, passwoed);
				} else {
					// 没有账号密码去登录 页面
					myHandler.sendEmptyMessageDelayed(2, 1000);
				}
			} else {
				// 去登录页面
				myHandler.sendEmptyMessageDelayed(2, 1000);
			}
		}
	}

	private void checkPreferncesWorkState() {
		boolean running = Preferences.isFirstRunning(SplashScreenActivity.this);
		String username = Preferences.getUserName(SplashScreenActivity.this);
		if (running && !TextUtils.isEmpty(username)) {
			CrashHandler.savaInfoToSD(this, "crash","Prefernces检查", "checkPreferncesWorkState\n"
					+ "isFirstRunning is true ,but username not null\n" + "running value is:" + running + "\n" + "username value is:"
					+ username);
		} else {

		}
	}

	private void login(String username, String passwoed) {
		ServerApiFactory.getApiImpl().login(username, passwoed, new RequestCallback() {

			@Override
			public void onSuccess(String rawData, Object response) {
				LoginResponse baseResponse = (LoginResponse) response;
				// fix: 一次性commit
//				Editor editor = Preferences.getPreferences(SplashScreenActivity.this).edit();
				if (baseResponse.getStatus().equals(Constants.REQUEST_SUCCESS)) {
					 Preferences.saveLoginSign(SplashScreenActivity.this,
					 baseResponse.getSign());
					 Preferences.saveLoginStatus(SplashScreenActivity.this,
					 true);
//					editor.putString(Preferences.LOGIN_SIGN, baseResponse.getSign());
//					editor.putBoolean(Preferences.ISLOGIN, true);
					if (Preferences.isFromPush(SplashScreenActivity.this)) {
						DBLDebug.logi("to main 45");
						Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						startActivity(intent);
					} else {
						// auto login
						int what = getIntent().getIntExtra("what", MainActivity.WHAT_DEFAULT);
						if (what == MainActivity.WHAT_FROM_BLE_SERVICE) {
							DBLDebug.logi("to main 46");
							MainActivity.launch(SplashScreenActivity.this, MainActivity.WHAT_FROM_BLE_SERVICE);
						} else {
							DBLDebug.logi("to main 47");
							MainActivity.launch(SplashScreenActivity.this);
						}
					}
				} else {
					if (Preferences.isFromPush(SplashScreenActivity.this)) {
						Intent intent = new Intent(SplashScreenActivity.this, LoginActivity.class);
						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						startActivity(intent);
					} else {
						LoginActivity.launch(SplashScreenActivity.this);
					}
					 Preferences.saveLoginSign(SplashScreenActivity.this, "");
//					editor.putString(Preferences.LOGIN_SIGN, "");
					showToast(baseResponse.getDesc());
				}
				 Preferences.saveFromPush(SplashScreenActivity.this, false);
//				editor.putBoolean(Preferences.FROM_PUSH, false);
//				editor.commit();
				SplashScreenActivity.this.finish();
			}

			@Override
			public void onError(int statusCode, Throwable e, String rawData) {
				if (Preferences.isFromPush(SplashScreenActivity.this)) {
					Intent intent = new Intent(SplashScreenActivity.this, LoginActivity.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivity(intent);
				} else {
					LoginActivity.launch(SplashScreenActivity.this);
				}
				Preferences.saveFromPush(SplashScreenActivity.this, false);
				if (NetworkUtils.isNetConnect(getApplicationContext())) {
					showToast(R.string.oper_error);
				} else {
					showToast(R.string.net_error);
				}
				SplashScreenActivity.this.finish();
			}
		});
	}

}
